<?php

	function deleteAlbum($id)
	{
		$sql = "SELECT id
				FROM photo
				WHERE album=$id;";
		
		$rh = query($sql);
		
		while ( $photo = mysql_fetch_array($rh) )
		
			deletePhoto( $photo[0] );
		
		$sql = "DELETE FROM album WHERE id=$id;";
		query($sql);
	}
	
	function deletePhoto($id)
	{
		$sql = "SELECT id,type
				FROM photo
				WHERE id=$id;";
		
		$rh = query($sql);
		
		if ( mysql_num_rows($rh)>0 )
		{
			query("DELETE FROM photo WHERE id=$id;");
			
			list($id, $type) = mysql_fetch_array($rh);
			
			unlink("../Thumbs/$id-small.jpg");
			unlink("../Thumbs/$id-medium.jpg");
			unlink("../Thumbs/$id-square.jpg");
			unlink("../Photos/$id.$type");
		}
	}
	
	function createAlbum($userId, $name)
	{
		$path = formatWords($name);
		
		$sql = "INSERT INTO album (user,path,name,creationdate) VALUES ($userId,'$path','$name',NOW());";
		
		query($sql);
		
		return mysql_insert_id();
	}
	
	function renameAlbum($id, $toName)
	{
		$path = formatWords($toName);
		
		$sql = "UPDATE album
				SET name='$toName',
				path='$path'
				WHERE id=$id;";
		
		query($sql);
	}
	
	function renamePhoto($id, $toName)
	{
		$path = formatWords($toName);
		
		$sql = "UPDATE photo
				SET name='$toName',
				path='$path'
				WHERE id=$id;";
		
		query($sql);
	}
	
	function isValidAlbumName($name)
	{
		$path = formatWords($name);
		
		return (! empty($path) );
	}
	
	function isValidPhotoName($name)
	{
		$path = formatWords($name);
		
		return (! empty($path) );
	}
	
	function getAlbumByUserAndName($userid, $name)
	{
		$path = formatWords($name);
		
		$sql = "SELECT * FROM album WHERE user=$userid AND path='$path';";
		
		return mysql_fetch_assoc( query($sql) );
	}
	
	function getPhotoByAlbumAndName($albumId, $name)
	{
		$path = formatWords($name);
		
		$sql = "SELECT * FROM photo WHERE album=$albumId AND path='$path';";
		
		return mysql_fetch_assoc( query($sql) );
	}
	
	function getAlbumById($albumId)
	{
		$sql = "SELECT * FROM album WHERE id='$albumId';";
		
		return mysql_fetch_assoc( query($sql) );
	}
	
	function getPhotoById($photoId)
	{
		$sql = "SELECT * FROM photo WHERE id='$photoId';";
		
		return mysql_fetch_assoc( query($sql) );
	}
	
	function albumExists($userid, $name)
	{
		$path = formatWords($name);
		
		$sql = "SELECT id FROM album WHERE user=$userid AND path='$path';";
		
		return mysql_num_rows( query($sql) )>0;
	}
	
	function setAlbumDescription($albumId, $description)
	{
		$sql = "UPDATE album SET description='$description' WHERE id=$albumId";
		
		query($sql);
	}
	
	function setPhotoDescription($photoId, $description)
	{
		$sql = "UPDATE photo SET description='$description' WHERE id=$photoId";
		
		query($sql);
	}
	
	function getUserAlbums($id)
	{
		$sql = "SELECT a.id AS 'aid',p.id AS 'pid',p.type,a.name,a.path,COUNT(p.id) AS 'photoCount'
				FROM album a
				LEFT JOIN photo p ON p.album=a.id
				WHERE a.user=$id
				GROUP BY a.id
				ORDER BY a.name;";
		
		$albums = selectQuery($sql);
		
		return $albums;
	}
	
	function getLatestUserPhotos($id)
	{
		$sql = "SELECT p.*,a.path AS 'albumPath'
				FROM photo p
				JOIN album a ON a.id=p.album
				WHERE p.user=$id
				ORDER BY p.id DESC
				LIMIT 6;";
		
		return selectQuery($sql);
	}
	
	function getLatestPhotos($num = 5, $uniq = 0)
	{
		$add = ($uniq == 1) ? 'WHERE p.id = (SELECT MAX(id) FROM photo WHERE user = u.id)' : '';
		$sql = "SELECT p.*,a.path AS 'albumPath',u.username, YEAR(CURDATE()) - YEAR(prf.birthday) - IF( RIGHT(CURDATE(),5) < RIGHT(prf.birthday,5) , 1, 0 ) AS 'age', c.name AS 'country', prf.gender
				FROM photo p
				JOIN album a ON a.id=p.album
				JOIN user u ON a.user=u.id
				JOIN profile prf ON prf.user=u.id AND prf.current=1
				JOIN region c ON prf.country=c.id
				$add
				ORDER BY p.id DESC
				LIMIT $num;";
		
		return selectQuery($sql);
	}

?>